import asyncio
from pyppeteer import launch

"""
这里用到了evaluate，另外查看exposeFunction、evaluateOnNewDocument、evaluateHandle 的用法
"""
width, height = 1366, 768


async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.setViewport({'width': width, 'height': height})
    await page.goto('https://dynamic2.scrape.center/')
    await page.waitForSelector('.item .name')  # waitForSelector 如果找到元素就返回。选择器是css
    await asyncio.sleep(2)
    await page.screenshot(path='截图2.png')
    dimensions = await page.evaluate('''()=>{
     return {
     width: document.documentElement.clientWidth,
     height: document.documentElement.clientHeight,
     deviceScaleFactor: window.devicePixelRatio,
     }
    }''')

    print(dimensions)
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
